package com.coffeeloversupportlibrary.coffeelover.domain.article.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cleveroad.fanlayoutmanager.FanLayoutManager;
import com.cleveroad.fanlayoutmanager.FanLayoutManagerSettings;
import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.domain.article.entity.Article;
import com.coffeeloversupportlibrary.coffeelover.domain.article.type.RecommendationType;
import com.coffeeloversupportlibrary.databinding.RecommendationShowBinding;

import java.util.List;

/**
 * @author puyv
 * @createTime 2021/11/6 20:16
 * @projectName CoffeeLoverSupportLibrary
 * @className RecommendationAdapter.java
 * @description TODO
 */
public class RecommendationAdapter implements Adapter<List<RecommendationType>> {
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.recommendation_show,parent,false);
        RecommendationShowBinding recommendationShowBinding=RecommendationShowBinding.bind(view);
        return new RecommendationArticleViewHolder(recommendationShowBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull List<RecommendationType> items, @NonNull RecyclerView.ViewHolder holder, int position) {
        RecommendationType item=items.get(position);
        List<Article> list=item.getItems();

        FanLayoutManagerSettings fanLayoutManagerSettings = FanLayoutManagerSettings
                .newBuilder( ((RecommendationArticleViewHolder)holder)
                        .recommendationShowBinding.getRoot().getContext())
                .withFanRadius(true)
                .withAngleItemBounce(1)
                .withViewWidthDp(120)
                .withViewHeightDp(160)
                .build();

        FanLayoutManager fanLayoutManager=new FanLayoutManager(((RecommendationArticleViewHolder)holder).recommendationShowBinding.getRoot().getContext(),fanLayoutManagerSettings);
        fanLayoutManager.scrollToPosition(5);
        RecommendationListAdapter recommendationListAdapter=new RecommendationListAdapter(list,fanLayoutManager,((RecommendationArticleViewHolder)holder).recommendationShowBinding.RecommendationItemView);
        ((RecommendationArticleViewHolder)holder).recommendationShowBinding.RecommendationItemView.setAdapter(recommendationListAdapter);
        ((RecommendationArticleViewHolder)holder).recommendationShowBinding.RecommendationItemView.setLayoutManager(fanLayoutManager);

    }

    static class RecommendationArticleViewHolder extends RecyclerView.ViewHolder{
        private RecommendationShowBinding recommendationShowBinding;
        public RecommendationArticleViewHolder(@NonNull RecommendationShowBinding recommendationShowBinding) {
            super(recommendationShowBinding.getRoot());
            this.recommendationShowBinding=recommendationShowBinding;
        }
    }
}


